local M = _G.ClientOHOH.class.new(script,_G.ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name)
local ClientOHOH = _G.ClientOHOH
local HomeGui = ClientOHOH.load.UIManager():CreateUI("HomeGui",require(script.HomeGui))
local CallBackManager = ClientOHOH.load.CallBackManager();
local UIManager = ClientOHOH.load.UIManager();
local Good_Cfg=ClientOHOH.config.Goods
local EventList = ClientOHOH.load.EventList();
local FunctionList = ClientOHOH.load.FunctionList();
local gamepassGid = {
    ["quickRollSpeed"] =1,
    ["luck2"] = 2,
    ["luck4"] = 3,
    ["doubleRoll"] = 5
}
function M.new(self,...)
    return M(self,...)
end

function M.ctor(self,parent)
    M.super.ctor(self,parent)
    self.DataStore = self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
    self.player = parent
    HomeGui:Show()
    local data = self.DataStore:GetData("base")
    self.AttributeCtrl = self.parent:GetComponent(_G.CommonOHOH.load.Common_CarInfoCtrl())
    self.isAllBuy = 0 --是否买了主界面所有的gamepass
    self.lastBuffData = self.DataStore:GetData("buff") --上一次的buff数据
    --设置主界面售卖的gamepass数据
    self.gamepassData = {}
    for name, goodId in pairs(gamepassGid) do
        for id, value in pairs(Good_Cfg[goodId].itemData) do
            self.gamepassData[name] = {
                gid = goodId,
                gName = id
            }
        end
    end
    HomeGui:SetGamepassData(self.gamepassData)


    HomeGui:SetInfo(data)
    self:AddConnect()
end

function M.AddConnect(self)
    --更新info
    self.player.UserDataEvent:Connect(function(data)
        if data["base"] then
            self:UpdateInfoPanel()
        end
        if data["gamePass"] and self.isAllBuy < 4 then
            self.isAllBuy = 0
            local gamepass = data["gamePass"]
            for id, _ in pairs(gamepass) do
                self.isAllBuy =self.isAllBuy+HomeGui:UpdateGamepass(id)
            end
        end
        if data["buff"] then
            local buffData = data["buff"] 
            local count = 0
            for index, value in pairs(buffData) do
                if self.lastBuffData[index] then
                    if self.lastBuffData[index]<value then
                        count = count+value - self.lastBuffData[index]
                    end
                else
                    count = count +value
                end
            end
            self.lastBuffData = buffData
            HomeGui:SetPotionRed(count)
        end
    end)
    
    HomeGui.buyEvent:Connect(function(gid)
        self:BuyGamePass(gid)
    end)

    --更新面板
    self.player.RngDataLayerUpdateEvent:Connect(function()
       self:UpdateInfoPanel()
    end)

end

function M.UpdateInfoPanel(self)
    if not self.RngDataCtrl then
        self.RngDataCtrl = self.player:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    end
    local data = {}
    data.Lucky = self.RngDataCtrl:GetLucky()
    data.RainbowLuck = self.RngDataCtrl:GetRainbowLucky()
    data.ShinyLuck = self.RngDataCtrl:GetShinyLucky()
    data.VoidLuck = self.RngDataCtrl:GetVoidLucky()
    data.RollSpeed = self.RngDataCtrl:GetRollSpeed()
    data.diamond = self.DataStore:GetData("base").diamond

    HomeGui:SetInfo(data)
end

function M.BuyGamePass(self,gid)
    self.player.RobuxBuyEvent:Action(gid)
end

function M.dtor(self)
    M.super.dtor(self)
end


return M 